package com.xunyin.modules.membersuser.service.mapper;

import com.xunyin.common.mapper.CoreMapper;
import com.xunyin.modules.membersuser.domain.TMembersUser;
import com.xunyin.modules.membersuser.vo.MemberUserVo;
import com.xunyin.modules.membersuser.vo.MembersDiscountVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-09-22
 */
@Repository
public interface TMembersUserMapper extends CoreMapper<TMembersUser> {

    @Select("SELECT smu.id,smu.main_id,smu.members_id,smu.members_time_length,smu.members_trans_count," +
            " smu.member_user_phone,smu.create_time, smu.update_time,smu.delete_time,smu.create_user_id," +
            " smu.update_user_id,smu.status,sm.members_level" +
            " FROM t_members_user smu " +
            " left join t_members sm on sm.id = smu.members_id " +
            "  where smu.member_user_phone = #{phone} " +
            "  and smu.main_id = #{mainId} ")
        //"  and sm.status=1 AND smu.status = 1 ")
    MemberUserVo selectByTransPhoneAndMainId(@Param("phone") String phone, @Param("mainId") Integer mainId);


    @Select("select smu.id,sm.id AS members_id,sm.members_name,sm.members_level," +
            "smu.members_time_length,sm.members_discount,sm.main_id AS main_id," +
            "smu.members_trans_count, smu.member_user_phone, sm.create_time, sm.update_time," +
            "sm.delete_time, sm.create_user_id, sm.update_user_id, sm.status," +
            "GROUP_CONCAT(sth.id) AS tea_house_id_array," +
            "GROUP_CONCAT(sth. NAME) AS tea_house_name_array" +
            " FROM t_members_user smu " +
            " LEFT JOIN t_members sm ON smu.members_id = sm.id" +
            " LEFT JOIN t_store sth ON sth.main_id = smu.main_id" +
            //" WHERE smu.member_user_phone = #{phone} AND smu.status = 1 AND sm.status = 1 AND sth.status = 1" +
            " WHERE smu.member_user_phone = #{phone} AND sth.status = 1" +
            " AND smu.main_id = #{mainId}" +
            " group by smu.id")
    List<MemberUserVo> selectByUserIdAndMainId(@Param("phone") String phone, @Param("mainId") Integer mainId);


    @Select("select  u.id membersUserId, m.members_main_name membersMainName, m.members_discount membersDiscount from " +
            " t_members_user u" +
            " INNER JOIN t_members m ON u.members_id=m.id" +
            " where u.`status`=1" +
            " AND m.`status`=1" +
            " AND u.main_id= #{mainId}" +
            " AND u.member_user_phone=#{memberUserPhone}")
    MembersDiscountVo getMembersDiscountByMainIdAndPhone(@Param("mainId") Integer mainId, @Param("memberUserPhone") String memberUserPhone);

}
